
package com.android.finaltracer.model;

import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class DataFormatFactory {
    private final static String TAG = DataFormatFactory.class.getSimpleName();

    private DataFormatFactory() {

    }

    public static DataFormatInterface createInstance(String jsonStr) {
        DataFormatInterface ret = null;
        try {
            JSONObject json = new JSONObject(jsonStr);
            int type = json.getInt(DataFormatBasic.TAG_TYPE);
            switch (type) {
                case DataFormatBasic.TYPE_POSITION_INFO:
                    ret = new PositionInfo();
                    break;
                case DataFormatBasic.TYPE_COMMAND_INFO:
                    ret = new CommandInfo();
                    break;
                case DataFormatBasic.TYPE_BIND_INFO:
                    ret = new UserBindInfo();
                    break;
                case DataFormatBasic.TYPE_SYNC_INFO:
                    ret = new SyncInfo();
                    break;
                default:
                    break;
            }
            ret.decode(json.getString(DataFormatBasic.TAG_BODY));
        } catch (JSONException e) {
            Log.e(TAG, "createInstance error", e);
        }
        return ret;
    }
}
